<template>
  <el-container>
    <el-header><Header /></el-header>
    <el-main>
      <div class="left">
        <el-card>
          <div slot="header">
            申请成为心理咨询师
          </div>
          <div v-if="userInProcess">
            <el-steps align-center :active="2">
              <el-step title="申请" description="提交申请证明" />
              <el-step title="管理员审批" description="等待管理员审批,这个时间可能需要1-2天" />
              <el-step title="结果" description="等待管理员审批,这个时间可能需要1-2天" />
            </el-steps>
          </div>
          <div v-else-if="user_isConsultant === 0" class="uploadDiv">
            <el-upload
              ref="upload"
              class="upload-demo"
              drag
              action="http://localhost:8081/test/user/toBeConsultant/"
              :data="{ userId: user_id }"
              :auto-upload="false"
              :file-list="fileList"
              :on-change="handleChange"
              :on-success="handleSuccess"
              :on-error="handleError"
              multiple
            >
              <i class="el-icon-upload" />
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div slot="tip" class="el-upload__tip">可上传.jpg/.png图片以及.pdf文件</div>
            </el-upload>
            <el-form v-model="form" class="form">
              <el-form-item label="选择您所擅长的领域">
                <el-select
                  v-model="value2"
                  multiple
                  collapse-tags
                  style="margin-left: 20px;"
                  placeholder="请选择"
                  @change="changeSelect"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.value"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-form>
            <el-button type="primary" @click="submit">申请</el-button>
          </div>
          <div v-else-if="user_id === ''">
            <el-empty description="需要登录才能申请" />
          </div>
          <div v-else>
            <el-empty description="您已经是咨询师!无需再次申请." />
          </div>
          <div v-show="user_id !== ''" class="record">
            <div>
              申请记录
            </div>
            <div v-for="endTheProcess in list" :key="endTheProcess.createTime" class="processList">
              <div>{{ endTheProcess.createTime }}</div>
              <el-steps align-center :active="active" :finish-status="endTheProcess.isPass === 1 ? 'success' : 'error'">
                <el-step title="申请" status="success" description="提交申请证明" />
                <el-step title="管理员审批" status="success" description="等待管理员审批,这个时间可能需要1-2天" />
                <el-step v-if="endTheProcess.isPass === 1" title="通过" description="恭喜您通过" />
                <el-step v-else title="结束" description="很遗憾,您的申请被驳回" />
              </el-steps>
            </div>
          </div>
        </el-card>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import { mapGetters } from 'vuex'
import Header from '../header/index.vue'
import { uploadFile } from '@/api/user'
import { getApprovList, isUserInProcess, getField } from '@/api/application'
export default {
  components: { Header },
  data() {
    return {
      active: 3,
      status: 'success',
      show: false,
      fileList: [],
      list: [],
      form: {},
      options: [],
      skillField: [],
      value2: '',
      userInProcess: Boolean // 用户是否已经发起了流程
    }
  },
  computed: {
    ...mapGetters([
      'user_id',
      'user_isConsultant'
    ])
  },
  mounted() {
    this.getApprovList()
    this.isUserInProcess()
    this.getField()
  },
  methods: {
    isUserInProcess() {
      isUserInProcess({ userId: this.user_id }).then(res => {
        this.userInProcess = res.data
      })
    },
    handleChange(file, fileList) {
      this.fileList = fileList.slice(-3)
    },
    handleSuccess(response, file, fileList) {
      this.$notify({
        type: 'success',
        message: '上传成功',
        title: '成功'
      })
    },
    handleError(response, file, fileList) {
      this.$notify.error({
        title: '上传失败'
      })
    },
    submit() {
      if (this.fileList.length === 0) {
        this.$notify.info({
          message: '还没有选择文件噢!'
        })
        return
      } else {
        const formData = new FormData()
        this.fileList.forEach(file => {
          formData.append('file', file.raw)
        })
        formData.append('userId', this.user_id)
        formData.append('skillField', this.skillField)
        uploadFile(formData).then(res => {
          if (res.code === 1) {
            this.$notify({
              type: 'success',
              title: '上传成功'
            })
            this.isUserInProcess()
          } else {
            this.$notify.error({
              title: '上传失败'
            })
          }
        })
      }
    },
    getApprovList() {
      getApprovList({ userId: this.user_id, username: null, startTime: null, endTime: null }).then(res => {
        this.list = res.data.list
      })
    },
    getField() {
      getField().then(res => {
        this.options = res.data
      })
    },
    changeSelect(value) {
      this.skillField = value
      console.log(this.skillField)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-container{
  height: 100vh;
}
.el-header {
  padding: 0;
}
.el-main {
  padding: 20px;
  background: #5f8ebd;
  .el-card{
    width: 70%;
    height: inherit;
    background: #f3f4f5;
  }
}
.uploadDiv{
  flex: justify-content;
  .upload-demo{
    width: 360px
  }
  .el-button {
    width: 360px;
    margin-top: 20px;
  }
}
.record{
  margin-top: 30px;
}
.processList {
  margin-left: 10px;
  margin-top: 40px;
}
.form {
  margin-top: 20px;
}
</style>>

